package edu.hrbeu.controller;

import edu.hrbeu.entity.User;
import edu.hrbeu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author wzq
 * @create 2021-12-16-10:37
 */
@Controller
public class IndexController {

    @RequestMapping(value = {"/welcome", "/index","/"})
    @ResponseBody
    public String index(String name){
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("你好,");
        stringBuffer.append(name);
        stringBuffer.append("<br>");
        stringBuffer.append("欢迎使用springboot");
        return stringBuffer.toString();
    }

    @GetMapping("/login")
    public void login(){

    }
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public String login(HttpSession session,String username, String password, Model model){
//        if(username!=null&&!"".equals(username.trim())&&"000000".equals(password)){
//            session.setAttribute("username",username);
//            return "redirect:/main";
        User loginuser = userService.login(username,password);
        if(loginuser!=null){
            session.setAttribute("loginUser",loginuser);
            return "redirect:/book/list";
        }else {
            model.addAttribute("login_error","用户名或密码错误，请重试");
            return "login";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login";
    }


    @RequestMapping("/main")
    public String main(){
        return "main";
    }
}
